package com.stars.empoly.utils

import android.content.Context
import android.os.Build
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import androidx.recyclerview.widget.RecyclerView
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.layout.QMUIFrameLayout
import com.qmuiteam.qmui.skin.QMUISkinValueBuilder
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.popup.QMUIPopup
import com.qmuiteam.qmui.widget.popup.QMUIPopups
import com.stars.empoly.adapter.AllCarAdapter
import com.stars.empoly.R
import com.stars.empoly.inte.ChoiceData
import com.stars.empoly.model.DataInfoBean
import com.trailer.base.utils.UiUtils

/**
 * 货主-选择车的类型
 *
 *on 2020/9/21
 */
object PopuAllCarsUtils {

    fun show(context: Context, view: View, list: ArrayList<DataInfoBean.CarTypeBean>, choiceData: ChoiceData) {

        var mNormalPopup = QMUIPopups.fullScreenPopup(context)
        val builder: QMUISkinValueBuilder = QMUISkinValueBuilder.acquire()
        val frameLayout = QMUIFrameLayout(context)
        //自定义布局
        val layoutInflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        var layout = layoutInflater.inflate(R.layout.item_all_car, null)

        val imgClose: ImageView = layout.findViewById(R.id.img_close)
        val recyclerView: RecyclerView = layout.findViewById(R.id.recycler_view)

        val tv: TextView = layout.findViewById(R.id.tv)

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { //不支持沉浸式状态栏
        } else {
            //获取状态栏的高度
            val statusBarHeight: Int = UiUtils.getStatusBarHeight()

            //动态设置高度  tv
            val params = tv.getLayoutParams()
            //获取当前控件的布局对象
            //params.height=UiUtils.dp2dip(getContext(), statusBarHeight);//设置当前控件布局的高度
            params.height = statusBarHeight
            tv.layoutParams = params //将设置好的布局参数应用到控件中
            tv.visibility = View.VISIBLE
        }

        var chose = object : ChoiceData {
            override fun onRestuse(id: String?) {
                choiceData?.onRestuse(id)

                mNormalPopup.dismiss()

            }

        }

        recyclerView.addItemDecoration(GridSpaceItemDecoration(3,
                UiUtils.dp2Px(context, 10f),
                UiUtils.dp2Px(context, 10f)))

        var adapter: AllCarAdapter = AllCarAdapter(chose)
        recyclerView.adapter = adapter
        adapter.addData(list)


        imgClose.onClick {
            mNormalPopup.dismiss()
        }
//
//        tvSure.onClick {
//
//            choiceData?.onRestuse("")
//
//            mNormalPopup.dismiss()
//        }


        builder.release();

        val size = QMUIDisplayHelper.getScreenWidth(context)

        val height = QMUIDisplayHelper.getScreenHeight(context)


        val lp: FrameLayout.LayoutParams = FrameLayout.LayoutParams(size,
                height)
        frameLayout.addView(layout, lp)

        mNormalPopup.addView(frameLayout)
        mNormalPopup.onDismiss() {
        }

        mNormalPopup.dismissIfOutsideTouch(false)
        mNormalPopup.animStyle(QMUIPopup.ANIM_AUTO)
        mNormalPopup.show(view)


    }
}
